﻿using System;
using System.IO;
using ANote = Microsoft.Office.Interop.OneNote;
using System.Linq;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.Xml;

namespace Pokerist
{
    public class OCRTools2010
    {
        //public string UpdateValues(string aFirstImageString, string aSecondImageString)
        //{
        //    String outputXML = default(string);

        //    try
        //    {
        //        // Init, Load Hierarchy and Establish Namespace
        //        var oneNoteApplication = new ANote.Application();
        //        oneNoteApplication.GetHierarchy("", ANote.HierarchyScope.hsPages, out outputXML);

        //        var oneNoteHierarchy = XDocument.Parse(outputXML);
        //        XNamespace oneNoteNameSpace = ONENOTE_NAMESPACE;

        //        // Loop through the books
        //        var noteBooks = from p in oneNoteHierarchy.Root.Elements(oneNoteNameSpace + "Notebook").Elements(oneNoteNameSpace + "Section").Elements()
        //                        select p;

        //        foreach (var book in noteBooks)
        //        {
        //            string pageID = (string)book.Attribute("ID");

        //            // Customise input string
        //            string pageXmlIn = myUpdateString.Replace("IMAGE_1", aFirstImageString);
        //            pageXmlIn = pageXmlIn.Replace("IMAGE_2", aSecondImageString);
        //            pageXmlIn = pageXmlIn.Replace("PAGE_ID", pageID);

        //            // Voila
        //            oneNoteApplication.UpdatePageContent(pageXmlIn, DateTime.MinValue);
        //        }

        //        return "";
        //    }
        //    catch (Exception e)
        //    {
        //        return e.Message;
        //    }
        //}


        //public string RetrieveValues(out double aFirstValue, out double aSecondValue)
        //{
        //    aFirstValue = 0.0;
        //    aSecondValue = 0.0;
        //    String outputXML = default(string);

        //    try
        //    {
        //        // Init, Load Hierarchy and Establish Namespace
        //        var oneNoteApplication = new ANote.Application();
        //        oneNoteApplication.GetHierarchy("", ANote.HierarchyScope.hsPages, out outputXML);

        //        var oneNoteHierarchy = XDocument.Parse(outputXML);
        //        XNamespace oneNoteNameSpace = ONENOTE_NAMESPACE;

        //        // Loop through the books
        //        var noteBooks = from p in oneNoteHierarchy.Root.Elements(oneNoteNameSpace + "Notebook").Elements(oneNoteNameSpace + "Section").Elements()
        //                        select p;

        //        int counter = 0;
        //        foreach (var book in noteBooks)
        //        {
        //            string pageID = (string)book.Attribute("ID");

        //            // Now Read the OCR'd data from the image, first get the page
        //            string pageXmlOut = default(string);
        //            oneNoteApplication.GetPageContent(pageID, out pageXmlOut, ANote.PageInfo.piBasic);

        //            // Using LINQ get all the OCRData Nodes
        //            var pageData = XDocument.Parse(pageXmlOut);
        //            var pngData =
        //                from x in
        //                    pageData.Root.Elements(oneNoteNameSpace + "Image")
        //                where (string)x.Attribute("format") == "png"
        //                select x.Element(oneNoteNameSpace + "OCRData");

        //            foreach (var ocrValue in pngData)
        //            {
        //                if (counter++ == 0)
        //                {
        //                    try
        //                    {
        //                        string temp = ocrValue.Value.Trim().Replace(" ", "");
        //                        aFirstValue = System.Convert.ToDouble(temp);
        //                    }
        //                    catch (Exception e)
        //                    {
        //                        return "Cannot find OCRDATA 1: " + e.Message;
        //                    }
        //                }
        //                else
        //                {
        //                    try
        //                    {
        //                        string temp = ocrValue.Value.Trim().Replace(" ", "");
        //                        aSecondValue = System.Convert.ToDouble(temp);
        //                    }
        //                    catch (Exception e)
        //                    {
        //                        return "Cannot find OCRDATA 2: " + e.Message;
        //                    }
        //                }
        //            }

        //            break;
        //        }

        //        return string.Empty;
        //    }
        //    catch (Exception e)
        //    {
        //        return e.Message;
        //    }
        //}


        //#region Private Variables and Constants

        //private const string ONENOTE_NAMESPACE = "http://schemas.microsoft.com/office/onenote/2010/onenote";

        //private string myUpdateString =
        //    "<?xml version=\"1.0\"?>" +
        //           "   <one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/2010/onenote\" ID=\"PAGE_ID\">" +
        //            "      <one:PageSettings RTL=\"false\" color=\"automatic\">" +
        //           "         <one:PageSize>" +
        //           "            <one:Automatic/>" +
        //           "         </one:PageSize>" +
        //           "         <one:RuleLines visible=\"false\"/>" +
        //           "      </one:PageSettings>" +
        //           "   <one:Image format=\"png\" originalPageNumber=\"0\" objectID=\"{9B0ED911-E091-48C3-B898-734C413BA656}{15}{B0}\">" +
        //           "   <one:Position x=\"198.0\" y=\"86.4000015258789\" z=\"0\"/>" +
        //           "   <one:Size width=\"52.4999885559082\" height=\"18.74999618530273\"/>" +
        //           "   <one:Data>IMAGE_1</one:Data>" +
        //           "   </one:Image>" +
        //           "   <one:Image format=\"png\" originalPageNumber=\"0\" objectID=\"{C37AA779-7517-4563-ACBB-7A961485F791}{11}{B0}\">" +
        //           "   <one:Position x=\"198.0\" y=\"122.400001525879\" z=\"1\"/>" +
        //           "   <one:Size width=\"52.50731658935547\" height=\"18.75259780883789\"/>" +
        //           "   <one:Data>IMAGE_2</one:Data>" +
        //           "   </one:Image>" +
        //           "   </one:Page>";

        //#endregion
    }
}